package com.changyi.device;

import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Build;
import android.os.Environment;


import com.changyi.device.printer.PrintManagerLib;
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.SunmiPrinterService;

import java.util.LinkedList;
import java.util.List;


/**
 * Created by Administrator on 2017/3/2.
 */

public class APApplication extends Application {
    private static APApplication mApplication;
    private List<Activity> activityList;
    public static SunmiPrinterService printerService = null;//商米标准打印 打印服务

    public static Context mCotext;


    private ProgressDialog mProgressDialog;
    public static APApplication getInstance(){
        return mApplication;
    }
    /**
     * SD卡根目录
     */
    private static String rootSDCardPath;
    /**
     * 应用专用数据文件夹
     */
    private static String rootFilesDirPath;
    /**
     * 应用专用缓存文件夹
     */
    private static String rootCacheDirPath;
    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        initFileState();
        mCotext = getApplicationContext();
        connectPrintService();

        mProgressDialog = new ProgressDialog(mCotext);
        activityList = new LinkedList<Activity>();
        ExceptionLog.getInstance().init(this);
        ExceptionLog.getInstance().setAutoRestart(true);
    }
    /**
     * 初始化文件环境
     */
    public void initFileState() {
        rootSDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            rootFilesDirPath = getExternalFilesDir(null).getAbsolutePath();
            rootCacheDirPath = getExternalCacheDir().getAbsolutePath();
        } else {
            rootFilesDirPath = getFilesDir().getAbsolutePath();
            rootCacheDirPath = getCacheDir().getAbsolutePath();
        }
    }

    /**
     * 获取SD卡根目录
     * @return
     */
    public static String getRootSDCardPath(){
        return rootSDCardPath;
    }
    /**
     * 获取应用的默认数据文件夹路径
     *
     * @return
     */
    public static String getRootFilesDirPath() {
        return rootFilesDirPath;
    }

    /**
     * 获取应用的默认缓存文件夹路径
     *
     * @return
     */
    public static String getRootCacheDirPath() {
        return rootCacheDirPath;
    }

    //添加Activity到容器中
    public void addActivity(Activity activity)  {
        activityList.add(activity);
    }

    //遍历所有Activity并finish
    public void exit() {
        for(Activity activity:activityList) {
            activity.finish();
        }
        activityList.clear();
    }

    //连接打印服务
    public void connectPrintService() {
        if (PrintManagerLib.deviceModelInit(Build.MODEL) == DeviceConstant.T2)
            try {
                InnerPrinterManager.getInstance().bindService(this, innerPrinterCallback);
            } catch (InnerPrinterException e) {
                e.printStackTrace();
            }
    }

    private InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
        @Override
        protected void onConnected(SunmiPrinterService service) {
            printerService = service;
        }

        @Override
        protected void onDisconnected() {
            printerService = null;

        }
    };
}
